package com.itheima.dao;

import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleDao {
/*    //根据用户id查询所有对应的角色
    @Select("select * from role where id in (select roleId from users_role where userId = #{userId})")
    @Results({
            @Result(property = "id" , column = "id"),
            @Result(property = "permissions" ,column = "id" ,many = @Many(select = "com.itheima.dao.PermissionDao.findPermissionByRoleId"))
    })*/

    //根据用户id查询出所有对应的角色
    @Select("select * from role where id in (select roleId from users_role where userId=#{userId})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "permissions",column = "id",
                    many = @Many(select = "com.itheima.dao.PermissionDao.findPermissionByRoleId"))
    })
    public List<Role> findRoleByUserId(String userId);


    /*
    查询所有角色信息
     */
    @Select("select * from role")
    public List<Role> findAll();

    /*
    添加角色
     */
    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    public void save(Role role);

    /*
    根据id查询角色信息
     */
    @Select("select * from role where id = #{id}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "permissions",column = "id" ,many = @Many(select = "com.itheima.dao.PermissionDao.findPermissionByRoleId"))
    })
    public Role findById(String id);


    /*
    查询可以添加的权限
     */
    @Select("select * from permission where id not in " +
            "(select permissionId from role_permission where roleId=#{roleId})")
    public List<Permission> findOtherPermissions(String roleId);

    @Insert("insert into role_permission(roleId,permissionId) values(#{roleId},#{permissionId})")
    public void addPermissionToRole(@Param("roleId") String roleId,@Param("permissionId") String permissionId);
}
